package hard;

public class Huakao1 {
    public static void main(String[] args) {
        int num = 3; // 正整数
        int maxLength = findLongestConsecutiveDigit(num);
        System.out.println("最长连续数字长度为：" + maxLength);
    }
    public static int findLongestConsecutiveDigit(long num) {
        if (num <= 0) {
            throw new IllegalArgumentException("输入必须是正整数。");
        }

        // 将长整数转换为二进制字符串
        String binaryString = Long.toBinaryString(num);

        // 初始化最大长度和当前长度
        int maxCount = 0;
        int currentCount = 0;

        // 遍历二进制字符串，查找最长连续数字长度
        char lastChar = ' '; // 初始化为非'0'或'1'
        for (int i = 0; i < binaryString.length(); i++) {
            char currentChar = binaryString.charAt(i);

            if (currentChar == lastChar) {
                currentCount++;
            } else {
                currentCount = 1; // 重新开始计数
                lastChar = currentChar;
            }

            // 更新最大长度
            if (currentCount > maxCount) {
                maxCount = currentCount;
            }
        }

        return maxCount;
    }
}
